//
//  JBWVisitorView.swift
//  新浪微博
//
//  Created by 季伯文 on 2017/6/29.
//  Copyright © 2017年 bowen. All rights reserved.
//

import UIKit
import SnapKit

class JBWVisitorView: UIView {

   //声明闭包
    var closure:(()->())?
    
    override init(frame: CGRect) {
        super .init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //监听事件
    @objc private func loginClick(){
        print("访客视图登录和注册按钮点击")
        //执行闭包
        closure?()
    }
    
    func setupVisitorViewInfo(imgName: String?, title: String?){
        
        //判断是否为首页
        if let img = imgName, let title = title {
            logoImageView.image = UIImage(named:img)
            messageLabel.text = title
            
            //隐藏转动圆圈
            circleImageView.isHidden = true
        } else {
            //首页
            setupCircleImageViewanim()
        }
    }
    //设置圆圈动画
    private func setupCircleImageViewanim(){
    
        //实例化动画对象
        let anim = CABasicAnimation (keyPath: "transform.rotation")
        //动画时长
        anim.duration = 10
        //tovalue
        anim.toValue = Double.pi * 2
        //重复次数
        anim.repeatCount = MAXFLOAT
        //当切换控制器或推到后台是 动画将会停止
        anim.isRemovedOnCompletion = false
        //添加动画
        circleImageView.layer.add(anim, forKey: nil)
    }
    
    //设置视图
    private func setupUI(){
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        
        //添加视图
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(logoImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //约束
        circleImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        logoImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(maskImageView.snp.bottom).offset(16)
        }
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottomMargin).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        loginButton.snp.makeConstraints { (make) in
            make.size.top.equalTo(registerButton)
            make.right.equalTo(messageLabel)
        }
    
    }
    
    //懒加载控件
    //圆圈
    lazy var circleImageView:UIImageView = UIImageView(named:"visitordiscover_feed_image_smallicon")
    //半透图
    lazy var maskImageView:UIImageView = UIImageView(named:"visitordiscover_feed_mask_smallicon")
    //大图标
    lazy var logoImageView:UIImageView = UIImageView(named:"visitordiscover_feed_image_house")
    //文字
    private lazy var messageLabel:UILabel = UILabel(text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", textColor: UIColor.darkGray, fontSize: JBWNormalFontSize, maxWeight: 230)
    // 注册按钮
    private lazy var registerButton: UIButton = UIButton(title: "注册", fontSize: JBWNormalFontSize, normalColor: UIColor.darkGray, hightlightedColor: JBWThemeColor, backgroundImgname: "common_button_white", target: self, action: #selector(loginClick))
    
    // 登录按钮
    private lazy var loginButton: UIButton =  UIButton(title: "登录", fontSize: 14, normalColor: UIColor.darkGray, hightlightedColor: UIColor.orange, backgroundImgname: "common_button_white", target: self, action: #selector(loginClick))
        
}
